/* $RCSFile: StaticCRMPage.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/StaticCRMPage.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

using AddonNice.Core;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Helpers;
using AddonNice.Security;
using AddonNice.Configuration;
using AddonNice.Design;
using AddonNice.Diagnostics;
using AddonNice.Settings;
using AddonNice.CRMWrapper;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.UI
{


	/// <summary>
	/// This Page is used as parent for pages loading AddonNice Modules but under their own models and with a static PageID
    /// Contains special methods and properties to avoid load of inappropriate modules on portalSettings load if pageid is not in request
	/// </summary>
	public class StaticCRMPage : CRMPage 
    {
        public bool FindPageIdInQueryString()
        {
            string[] queryStringValues;

            if ((Request.QueryString != null) && Request.QueryString.HasKeys() )
            {
                if (Request.QueryString[StStr.strPageID] != null)
                {
                    queryStringValues = Request.QueryString.GetValues(StStr.strPageID);
                    foreach(string str in queryStringValues)
                    {
                        if ( str == PageID.ToString() )
                            return true;
                    }
                }
            }
            return false;
        }

 
        protected override void  OnInit(EventArgs e)
        {
    		if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]StaticCRMPage OnInit BEG IsPostBack: {0}, QueryString: '{1}'",
                    IsPostBack,Request.Url.Query),PageTraceSwitch.Sw.Info);
            
            // To avoid any direct call
            if ( !FindPageIdInQueryString() )
            {
                string hostName     =   Request.Url.Host;
                Uri baseUri         =   new Uri(String.Format("{0}://{1}", Request.Url.Scheme,hostName));
                string path         =   Request.Url.AbsolutePath ;
                string query        =   string.Format("?{0}={1}",StStr.strPageID,PageID);
                if ( !string.IsNullOrEmpty(Request.Url.Query) )
                    query           =   query+"&"+Request.Url.Query.Substring(1);
                string newUrl       =   new Uri(baseUri, path+query).ToString();
    		    if (PageTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]StaticCRMPage OnInit missing PageId redirecting to: '{0}'",newUrl),PageTraceSwitch.Sw.Info);
                //Response.Redirect(newUrl);
                Redirect(newUrl,true);
                return;
            }
 	        base.OnInit(e);
		    if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]StaticCRMPage OnInit END Request.IsAuthenticated: {0}",Request.IsAuthenticated),PageTraceSwitch.Sw.Info);
        }

   }
}